package com.community.owner.controller;

import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.exception.ServiceException;
import com.community.owner.domain.Resident;
import com.community.owner.service.impl.ResidentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resident")
public class ResidentController extends BaseController {

    @Autowired
    private ResidentServiceImpl residentService;

    @GetMapping("/getResidentByOwnerId/{ownerId}")
    public AjaxResult getResidentByOwnerId(@PathVariable Long ownerId) {
        try {
            List<Resident> residents = residentService.getResidentByOwnerId(ownerId);
            return AjaxResult.success(residents);
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("获取住户信息失败", e);
            return AjaxResult.error("获取住户信息失败");
        }
    }

    //    * 批量新增家庭成员
    @PostMapping("/batchAddResidents")
    public AjaxResult batchAddResidents(@RequestBody List<Resident> residentList) {
        try {
            Integer result = residentService.batchAddResidents(residentList);
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
